//
//  SFitUtil.swift
//  适配工具类
//
//  Created by BrightLi on 2021/2/12.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

import Foundation
import UIKit

// 全局定义
public let ScreenWidth:CGFloat = SFitUtil.mainScreen().bounds.size.width
public let ScreenHeight:CGFloat = SFitUtil.mainScreen().bounds.size.height
public let ScreenMaxLength:CGFloat = max(ScreenWidth,ScreenHeight)
public let IsIPad = (UI_USER_INTERFACE_IDIOM() == .pad)
public let IsPhone = (UI_USER_INTERFACE_IDIOM() == .phone)
public let IsIPhoneX = (IsPhone && (ScreenMaxLength == 812.0 || ScreenMaxLength == 896.0))
public let IsIPhoneXR = (IsPhone && ScreenMaxLength == 896.0)
public let IsIPhone6P = (IsPhone && ScreenMaxLength == 736.0)
public let IsIPhone6 = (IsPhone && ScreenMaxLength == 667.0)
public let IsIPhone5 = (IsPhone && ScreenMaxLength == 568.0)
public let IsIPhone4OrLess = (IsPhone && ScreenMaxLength < 568.0)
public let LineHeight:CGFloat = (SFitUtil.mainScreen().scale>0.0 ?1/SFitUtil.mainScreen().scale:0.5)
public let SafeBottom:CGFloat = IsIPhoneX ?34:0
public let StatusBarHeight:CGFloat = IsIPhoneX ?44:20
public let NavBarHeight:CGFloat = 44

public let TabBarHeight:CGFloat = (IsIPhoneX ? 83 : 49)
public let TopBarHeight:CGFloat = StatusBarHeight+NavBarHeight

// 修正文本宽高
public let fixedWidth = SFitUtil.fit(3)
public let fixedHeight = SFitUtil.fit(3)
// 默认间距
public let Gap:CGFloat = SFitUtil.fit(15)

@objcMembers public class SFitUtil:NSObject
{
    // 计算一次屏幕绽放比例
    static var fitScale:CGFloat =
    {
        let w=SFitUtil.mainScreen().bounds.size.width
        let h=SFitUtil.mainScreen().bounds.size.height
        if(IsIPad){
            return w<h ?w/375/2:h/375/2;
        }else{
            return w<h ?w/375:h/375;
        }
    }()
    
    // 仅计算一次字体缩放比例
    static var fontScale:CGFloat = {
        var scale:CGFloat = 1.0
        if(IsIPhoneXR){
            scale=1.03
        }else if(IsIPhoneX){
            scale = 1.03
        }else if(IsIPhone6P){
            scale = 1.03
        }else if(IsIPhone6){
            scale = 1.0
        }else if(IsIPhone5){
            scale = 0.85
        }else if(IsIPhone4OrLess){
            scale = 0.8
        }else if(IsIPad){
            scale = 1.1
        }
        return scale
    }()
    
    dynamic public class var screenWidth:CGFloat
    {
        get{
            return SFitUtil.mainScreen().bounds.size.width
        }
    }
    
    dynamic public class var screenHeight:CGFloat
    {
        get{
            return SFitUtil.mainScreen().bounds.size.height
        }
    }
    dynamic public class var statusBarHeight:CGFloat
    {
        get{
            return StatusBarHeight
        }
    }
    dynamic public class var navBarHeight:CGFloat
    {
        get{
            return NavBarHeight
        }
    }
    dynamic public class var topBarHeight:CGFloat
    {
        get{
            return TopBarHeight
        }
    }
    dynamic public class var tabBarHeight:CGFloat
    {
        get{
            return TabBarHeight
        }
    }
    dynamic public class var safeBottom:CGFloat
        {
        get{
            return SafeBottom
        }
    }
    // 适配尺寸
    dynamic public class func fit(_ value:CGFloat) -> CGFloat
    {
        let result = CGFloat(value*fitScale)
        return round(result)
    }
    // 适配线尺寸
    dynamic public class func fitLine() -> CGFloat
    {
        return LineHeight
    }
    // 适配尺寸
    dynamic public class func fitSize(_ width:CGFloat,height:CGFloat) -> CGSize
    {
        return CGSize(width:width*fitScale,height:height*fitScale)
    }
    // 适配字体大小
    dynamic public class func fitFont(_ size:CGFloat,name: String) -> UIFont
    {
        let fitSize = size*fontScale
        return UIFont(name: name, size: fitSize) ?? SFitUtil.fitFont(fitSize)
    }
    // 适配字体大小
    dynamic public class func fitFont(_ size:CGFloat) -> UIFont
    {
        let fitSize = size*fontScale
        return UIFont.systemFont(ofSize: CGFloat(fitSize))
    }
    // 适配字体大小
    dynamic public class func fitFontSize(_ fontSize:CGFloat) -> CGFloat
    {
        return fontSize*fontScale
    }
    // 适配粗体字体大小
    dynamic public class func fitBoldFont(_ size:CGFloat) -> UIFont
    {
        let fitSize = round(size*fontScale)
        return UIFont.boldSystemFont(ofSize: fitSize)
    }
    // 适配字体大小
    dynamic public class func fitFont(_ name:String, size:CGFloat ) -> UIFont
    {
        let fitSize = round(size*fontScale)
        return UIFont(name:name,size:fitSize)!
    }
    // 获得文本控件尺寸
    dynamic public class func sizeOf(_ label:UILabel?) -> CGSize
    {
        return sizeOf(label, width:0)
    }
    // 获得文本控件宽度
    dynamic public class func widthOf(_ label:UILabel?) -> CGFloat
    {
        if label==nil{
            return 1
        }
        if label!.attributedText == nil || label!.text==nil{
            return 1
        }
        let size:CGSize = label!.sizeThatFits(CGSize.zero)
        let width = ceil(size.width + fixedWidth)
        return width
    }
    // 获得文本控件宽度
    dynamic public class func noFixedWidthOf(_ label:UILabel?) -> CGFloat
    {
        if label==nil{
            return 1
        }
        if label!.attributedText == nil || label!.text==nil{
            return 1
        }
        let size:CGSize = label!.sizeThatFits(CGSize.zero)
        let width = ceil(size.width)
        return width
    }
    // 获得文本控件高度
    dynamic public class func heightOf(_ label:UILabel?) -> CGFloat
    {
        if label==nil{
            return 1
        }
        if label!.attributedText == nil || label!.text==nil{
            return label!.font.pointSize
        }
        label!.layoutIfNeeded()
        let width=label!.frame.size.width
        let size:CGSize = label!.sizeThatFits(CGSize(width:width,height:0))
        let height = ceil(size.height + fixedHeight)
        return height
    }
    // 获得文本控件高度
    dynamic public class func heightOf(_ label:UILabel?,width:CGFloat) -> CGFloat
    {
        if label == nil{
            return 1
        }
        if label!.text == nil{
            return label!.font.pointSize
        }
        let size:CGSize! = label!.sizeThatFits(CGSize(width:width,height:0))
        let height = ceil(size.height + fixedHeight)
        return height
    }
    // 获得多个文本控件最大高度
    dynamic public class func heightOfLabels(_ list:Array<UILabel>) ->CGFloat
    {
        var height:CGFloat=0;
        for label:UILabel in list
        {
            height=max(height,self.heightOf(label))
        }
        return height
    }
    // 获得属性文本尺寸
    dynamic public class func sizeOfAtt(_ target:NSAttributedString) -> CGSize
    {
        let temp = UILabel()
        temp.numberOfLines=0
        temp.attributedText=target
        var size = temp.sizeThatFits(CGSize(width:0,height:0))
        size.width = ceil(size.width + fixedWidth)
        size.height = ceil(size.height + fixedHeight)
        return size
    }
    // 获得属性文本尺寸
    dynamic public class func sizeOfMutable(_ target:NSMutableAttributedString,width:CGFloat) -> CGSize
    {
        let temp=UILabel()
        temp.numberOfLines=0
        temp.attributedText=target
        var size = temp.sizeThatFits(CGSize(width:width,height:0))
        size.width = ceil(size.width + fixedWidth)
        size.height = ceil(size.height + fixedHeight)
        return size
    }
    // 获得文本控件尺寸
    dynamic public class func sizeOf(_ label:UILabel?,width:CGFloat) -> CGSize
    {
        if label == nil{
            return CGSize.zero
        }
        label!.text = label!.text ?? ""
        var size = label!.sizeThatFits(CGSize(width:width,height: 0))
        size.width = ceil(size.width + fixedWidth)
        size.height = ceil(size.height + fixedHeight)
        return size
    }
    dynamic public class func sizeOf(_ text:String,width:CGFloat,fontSize:CGFloat)->CGSize
    {
        let label = UILabel()
        label.text = text
        label.font = SFitUtil.fitFont(fontSize)
        label.numberOfLines = 0
        var size = label.sizeThatFits(CGSize(width:width,height:0))
        size.width = ceil(size.width + fixedWidth)
        size.height = ceil(size.height + fixedHeight)
        return size
    }
    
    //屏幕height比例
    static public var fitHeightScale:CGFloat =
    {
        return SFitUtil.mainScreen().bounds.size.height/667;
    }()
    
    // 适配height
    dynamic public class func fitHeight(_ value:CGFloat) -> CGFloat
    {
        return value*fitHeightScale
    }
    // 线高度
    @objc class var lineHeight:CGFloat{
        get{
            return LineHeight
        }
    }

    dynamic public class func mainScreen() -> UIScreen
    {
        if #available(iOS 13.0, *) {
            let screen = (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.screen
            if screen != nil {
                return screen!
            }
        }
        return UIScreen.main
    }
}
